package com.xdjmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.xdjmall.dao.AdminUserDao;
import com.xdjmall.entity.AdminUser;
import com.xdjmall.util.DBUtils;
import com.xdjmall.util.QueryCondition;

public class AdminUserDaoImpl implements AdminUserDao {

	@Override
	public Integer insert(AdminUser t) throws SQLException {
		// TODO 丽丽
		String sql = "insert into admin_user(login_user_name,login_password,nick_name) values(?,?,?)";
		try (Connection c = DBUtils.getConnection();
			PreparedStatement ps = c.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS)) {
			ps.setString(1, t.getLoginUserName());
			ps.setString(2, t.getLoginPassword());
			ps.setString(3, t.getNickName());
			int row = ps.executeUpdate();
			//得到返回的生成
			ResultSet keySet = ps.getGeneratedKeys();
			if (keySet.next()) {
				t.setAdminUserId(keySet.getInt(1));
			}
			return row;
		}
	}

	@Override
	public Integer delete(Integer id) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public Integer update(AdminUser t) throws SQLException {
		// TODO 丽丽
		String sql = "update admin_user set ";
		if (t.getLoginUserName() != null) {
			sql += " login_user_name=?,";
		}
		if (t.getLoginPassword() != null) {
			sql += " login_password=?,";
		}
		if (t.getNickName() != null) {
			sql += " nick_name=?,";
		}
		if (t.getLocked() != null) {
			sql += " locked=?,";
		}
		sql = sql.substring(0,sql.length()-1);
		sql += " where admin_user_id=?";
		
		try (Connection c = DBUtils.getConnection(); 
			 PreparedStatement ps = c.prepareStatement(sql);) {
			int num = 1;
			if (t.getLoginUserName() != null) {
				ps.setString(num++, t.getLoginUserName());
			}
			if (t.getLoginPassword() != null) {
				ps.setString(num++, t.getLoginPassword());
			}
			if (t.getNickName() != null) {
				ps.setString(num++, t.getNickName());
			}
			if (t.getLocked() != null) {
				ps.setBoolean(num++, t.getLocked());
			}
			ps.setInt(num, t.getAdminUserId());
			return ps.executeUpdate();
		}
	}

	@Override
	public AdminUser selectById(Integer id) throws SQLException {
		// TODO 珊珊
		String sql = "select * from admin_user where admin_user_id=? and locked=0";

		try(Connection c = DBUtils.getConnection();
			PreparedStatement ps = c.prepareStatement(sql)){
			//设置参数
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				AdminUser u =  new AdminUser();
				u.setAdminUserId(rs.getInt(1));
				u.setLoginUserName(rs.getString(2));
				u.setLoginPassword(rs.getString(3));
				u.setNickName(rs.getString(4));
				u.setLocked(rs.getBoolean(5));
				return u;
			}else {
				return null;
			}
		}	
	}

	@Override
	public Integer selectCount() throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public List<AdminUser> selectByPage(QueryCondition condition) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public AdminUser selectByLoginName(String loginName) throws SQLException {
		// TODO 珊珊
		AdminUser u = null;
		String sql = "select * from admin_user where login_user_name=? and locked=0";
		try(Connection c = DBUtils.getConnection();
			PreparedStatement ps = c.prepareStatement(sql)){
			//设置参数
			ps.setString(1, loginName);
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				u =  new AdminUser();
				u.setAdminUserId(rs.getInt(1));
				u.setLoginUserName(rs.getString(2));
				u.setLoginPassword(rs.getString(3));
				u.setNickName(rs.getString(4));
				u.setLocked(rs.getBoolean(5));
				return u;
			}else {
				return null;
			}
		}	
	}
}
